package com.ma.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ma.auth.mapper.SysUserMapper;
import com.ma.auth.service.SysUserService;
import com.ma.model.system.SysUser;
import com.ma.security.custom.LoginUserInfoHelper;
import com.ma.vo.system.SysUserQueryVo;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService{
    //实现用户的分页查询
    @Override
    public Page<SysUser> limit(Long page, Long limit, SysUserQueryVo sysUserQueryVo) {
        Page<SysUser> userPage = new Page<>(page,limit);
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        String username = sysUserQueryVo.getKeyword();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();
        if(!Strings.isEmpty(username)){
            queryWrapper.like(SysUser::getName,username);
        }
        if(!Strings.isEmpty(createTimeBegin)){
            queryWrapper.ge(SysUser::getCreateTime,createTimeBegin);//ge >=
        }
        if(!Strings.isEmpty(createTimeEnd)){
            queryWrapper.le(SysUser::getCreateTime,createTimeEnd); //le <=
        }
        return page(userPage, queryWrapper);
    }

    @Override
    public Boolean updateStatus(Long id, Integer status) {
        SysUser sysUser = baseMapper.selectById(id);
        sysUser.setStatus(status);
        int i = baseMapper.updateById(sysUser);
        return i != 0;
    }

    @Override
    public SysUser getByUsername(String username) {
        return this.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username));
    }

    @Override
    public Map<String, Object> getCurrentUser() {
        Long userId = LoginUserInfoHelper.getUserId();
        SysUser sysUser = this.getById(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("name", sysUser.getName());
        map.put("phone", sysUser.getPhone());
        return map;
    }
}
